<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="课程名称"></el-input>
    </el-form-item>
    <el-form-item label="封面，存放的是图片地址" prop="cover">
      <el-input v-model="dataForm.cover" placeholder="封面，存放的是图片地址"></el-input>
    </el-form-item>
    <el-form-item label="2级分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="2级分类id"></el-input>
    </el-form-item>
    <el-form-item label="1级分类id" prop="categoryParentId">
      <el-input v-model="dataForm.categoryParentId" placeholder="1级分类id"></el-input>
    </el-form-item>
    <el-form-item label="教师id" prop="teacherId">
      <el-input v-model="dataForm.teacherId" placeholder="教师id"></el-input>
    </el-form-item>
    <el-form-item label="是否免费 [0代表免费，1代表收费]" prop="isFree">
      <el-input v-model="dataForm.isFree" placeholder="是否免费 [0代表免费，1代表收费]"></el-input>
    </el-form-item>
    <el-form-item label="课程价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="课程价格"></el-input>
    </el-form-item>
    <el-form-item label="上架状态 [0代表未上架，1代表上架]" prop="isPublish">
      <el-input v-model="dataForm.isPublish" placeholder="上架状态 [0代表未上架，1代表上架]"></el-input>
    </el-form-item>
    <el-form-item label="购买人数" prop="buyCount">
      <el-input v-model="dataForm.buyCount" placeholder="购买人数"></el-input>
    </el-form-item>
    <el-form-item label="学习人数，免费课程的话只要观看一分钟则计数加1，而收费课程是购买过则计数加1,热度评分也是按学习人数来实现的" prop="learnCount">
      <el-input v-model="dataForm.learnCount" placeholder="学习人数，免费课程的话只要观看一分钟则计数加1，而收费课程是购买过则计数加1,热度评分也是按学习人数来实现的"></el-input>
    </el-form-item>
    <el-form-item label="课程等级 [1是初级，2是中级，3是高级]" prop="level">
      <el-input v-model="dataForm.level" placeholder="课程等级 [1是初级，2是中级，3是高级]"></el-input>
    </el-form-item>
    <el-form-item label="总课时，分钟为单位" prop="lessionMinute">
      <el-input v-model="dataForm.lessionMinute" placeholder="总课时，分钟为单位"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="modifier">
      <el-input v-model="dataForm.modifier" placeholder="修改人"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          cover: '',
          categoryId: '',
          categoryParentId: '',
          teacherId: '',
          isFree: '',
          price: '',
          isPublish: '',
          buyCount: '',
          learnCount: '',
          level: '',
          lessionMinute: '',
          createTime: '',
          creator: '',
          modifyTime: '',
          modifier: ''
        },
        dataRule: {
          name: [
            { required: true, message: '课程名称不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '封面，存放的是图片地址不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '2级分类id不能为空', trigger: 'blur' }
          ],
          categoryParentId: [
            { required: true, message: '1级分类id不能为空', trigger: 'blur' }
          ],
          teacherId: [
            { required: true, message: '教师id不能为空', trigger: 'blur' }
          ],
          isFree: [
            { required: true, message: '是否免费 [0代表免费，1代表收费]不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '课程价格不能为空', trigger: 'blur' }
          ],
          isPublish: [
            { required: true, message: '上架状态 [0代表未上架，1代表上架]不能为空', trigger: 'blur' }
          ],
          buyCount: [
            { required: true, message: '购买人数不能为空', trigger: 'blur' }
          ],
          learnCount: [
            { required: true, message: '学习人数，免费课程的话只要观看一分钟则计数加1，而收费课程是购买过则计数加1,热度评分也是按学习人数来实现的不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '课程等级 [1是初级，2是中级，3是高级]不能为空', trigger: 'blur' }
          ],
          lessionMinute: [
            { required: true, message: '总课时，分钟为单位不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          modifier: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/course/course/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.course.name
                this.dataForm.cover = data.course.cover
                this.dataForm.categoryId = data.course.categoryId
                this.dataForm.categoryParentId = data.course.categoryParentId
                this.dataForm.teacherId = data.course.teacherId
                this.dataForm.isFree = data.course.isFree
                this.dataForm.price = data.course.price
                this.dataForm.isPublish = data.course.isPublish
                this.dataForm.buyCount = data.course.buyCount
                this.dataForm.learnCount = data.course.learnCount
                this.dataForm.level = data.course.level
                this.dataForm.lessionMinute = data.course.lessionMinute
                this.dataForm.createTime = data.course.createTime
                this.dataForm.creator = data.course.creator
                this.dataForm.modifyTime = data.course.modifyTime
                this.dataForm.modifier = data.course.modifier
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/course/course/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'cover': this.dataForm.cover,
                'categoryId': this.dataForm.categoryId,
                'categoryParentId': this.dataForm.categoryParentId,
                'teacherId': this.dataForm.teacherId,
                'isFree': this.dataForm.isFree,
                'price': this.dataForm.price,
                'isPublish': this.dataForm.isPublish,
                'buyCount': this.dataForm.buyCount,
                'learnCount': this.dataForm.learnCount,
                'level': this.dataForm.level,
                'lessionMinute': this.dataForm.lessionMinute,
                'createTime': this.dataForm.createTime,
                'creator': this.dataForm.creator,
                'modifyTime': this.dataForm.modifyTime,
                'modifier': this.dataForm.modifier
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
